package com.longdaji.decoration.ui.captainApply;

import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.CaptainCheckBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.RxUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class CaptainApplyPresenter extends RxPresenter<CaptainApplyContract.View> implements CaptainApplyContract.Presenter {
    private final static String TAG = CaptainApplyPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public CaptainApplyPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void applyCaptain(String userId, String captainName, String dormitoryId, String telephone) {
        addSubscribe(mDataManager.applyCaptain(userId, captainName, dormitoryId, telephone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d(TAG, "HttpResponse is" + httpResponse);
                        if (mView == null) {
                            return;
                        }
                        if (TextUtils.equals("0", httpResponse.getStatusCode())) {
                            mView.showApplySuccess();
                        } else {
                            mView.showApplyFail(httpResponse.getMessage());
                        }

                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showApplyFail(msg);
                        }
                    }
                }));
    }

    @Override
    public void getApplyStatus(String userId) {
        addSubscribe(mDataManager.getCaptainApplyStatus(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<CaptainCheckBean>handleResult())
                .subscribeWith(new CommonSubscriber<CaptainCheckBean>() {
                    @Override
                    public void onSuccess(CaptainCheckBean checkBean) {
                        Log.d(TAG, "CaptainCheckBean is" + checkBean);
                        if (mView != null) {
                            if (checkBean != null) {
                                mView.getApplyStatusSuccess(checkBean.getCaptainCheck());
                            } else {
                                mView.getApplyStatusFail();
                            }
                        }

                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.getApplyStatusFail();
                        }
                    }
                }));
    }
}
